﻿#include "Deer.h"
#include "../../../Shared/Enums.h"
#include "../../../Shared/Functions/Functions.h"

using namespace Server::MirDatabase;

namespace Server::MirObjects::Monsters
{

    bool Deer::getCanAttack() const
    {
        return HarvestMonster::getCanAttack() && !_runAway;
    }

    Deer::Deer(MonsterInfo *info) : HarvestMonster(info)
    {
        if (Info->AI != 2)
        {
            return;
        }

        RemainingSkinCount = 5;

        if (getEnvir()->Random->Next(7) == 0)
        {
            _runAway = true;
            Quality = static_cast<short>(getEnvir()->Random->Next(8) * 2000);
            MoveSpeed -= 300;
        }
        else
        {
            Quality = static_cast<short>(getEnvir()->Random->Next(4) * 1000);
        }
    }

    void Deer::RefreshAll()
    {
        HarvestMonster::RefreshAll();

        if (_runAway && MoveSpeed >= 600)
        {
            MoveSpeed -= 300;
        }
    }

    void Deer::FindTarget()
    {
        if (_runAway)
        {
            HarvestMonster::FindTarget();
        }
    }

    void Deer::ProcessTarget()
    {
        if (_runAway)
        {
            if (!getCanMove() || getTarget() == nullptr)
            {
                return;
            }

            MirDirection dir = Functions::DirectionFromPoint(getTarget()->getCurrentLocation(), getCurrentLocation());

            if (Walk(dir))
            {
                return;
            }

            switch (getEnvir()->Random->Next(2)) //No favour
            {
                case 0:
                    for (int i = 0; i < 7; i++)
                    {
                        dir = Functions::NextDir(dir);

                        if (Walk(dir))
                        {
                            return;
                        }
                    }
                    break;
                default:
                    for (int i = 0; i < 7; i++)
                    {
                        dir = Functions::PreviousDir(dir);

                        if (Walk(dir))
                        {
                            return;
                        }
                    }
                    break;
            }
        }
        else
        {
            HarvestMonster::ProcessTarget();
        }
    }
}
